// ActionScript file
import mx.core.Container;

import org.flexlancer.Logger.Logger;
import org.flexlancer.container.NodesContainer;
import org.flexlancer.managers.SelectionManager;

private var _selected:Boolean;
private var _bgcolor:uint = 0xffffff;
private var _bgalpha:Number = 1;
private var _isOpen:Boolean = true;

private var _actual_width:Number;
private var _actual_height:Number;

public var nodes:NodesContainer = new NodesContainer;

private function OnCC():void{
	try{
		this.Selected = false;
		this.addEventListener(MouseEvent.MOUSE_DOWN,this.Me_OnMouseDown);
		
		this.nodes.percentHeight = 100;
		this.nodes.percentWidth = 100;
		
		this.nodes.x = 0;
		this.nodes.y = 0;
		
		this._actual_width = this.width;
		this._actual_height = this.height;
		//this.nodes.setStyle("backgroundColor",0);
		
		this.canItemsContainer.addChild(this.nodes);
		
		this.SetBG(this._bgcolor,this._bgalpha);
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function Me_OnMouseDown(evt:MouseEvent):void{
	try{
		evt.stopImmediatePropagation();
		SelectionManager.AddSelection(this);
		var prt:Container = Container(this.parent);
		var bound:Rectangle = new Rectangle(0,0,prt.width - this.width,prt.height - this.height);
		this.startDrag(false,bound);
		this.addEventListener(MouseEvent.MOUSE_UP,this.Me_OnMouseUp);
		this.addEventListener(MouseEvent.MOUSE_MOVE,this.Me_OnMouseMove);
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function Me_OnMouseUp(evt:MouseEvent):void{
	try{
		this.stopDrag();
		this.removeEventListener(MouseEvent.MOUSE_UP,this.Me_OnMouseUp);
		this.removeEventListener(MouseEvent.MOUSE_MOVE,this.Me_OnMouseMove);
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function Me_OnMouseMove(evt:MouseEvent):void{
	try{
		//this.Title = Math.round(this.x) + "," + Math.round(this.y);
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function Me_OnOpenClose():void{
	try{
		if ( this._isOpen ){
			this.btnOpenClose.label = "Open";
		}else{
			this.btnOpenClose.label = "Close";
		}
		this.OpenGroup(!this._isOpen);
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OpenGroup(open:Boolean=true):void{
	try{
		this._isOpen = open;
		
		if ( this._isOpen ){
			this.height = this._actual_height;
			this.width = this._actual_width;
		}else{
			this.height = 40;
			this.width = 200;
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}

public function SetBG(color:uint=0xffffff, alpha:Number=1 ):void{
	try{
		if ( this.MainContainer != null ){
			this.MainContainer.setStyle("backgroundColor",color);
			this.MainContainer.setStyle("backgroundAlpha",alpha);
		}
		this._bgalpha = alpha;
		this._bgcolor = color;
	}catch(err:Error){
		throw err;
	}
}

public function set Selected(val:Boolean):void{
	try{
		this._selected = val;
		this.ApplySelection();
	}catch(err:Error){
		throw err;
	}
}

public function get Selected():Boolean{
	return this._selected;
}

public function ApplySelection():void{
	try{
		this.anchorBL.visible = this._selected;
		this.anchorBM.visible = this._selected;
		this.anchorBR.visible = this._selected;
		this.anchorML.visible = this._selected;
		this.anchorMR.visible = this._selected;
		this.anchorTL.visible = this._selected;
		this.anchorTM.visible = this._selected;
		this.anchorTR.visible = this._selected;
	}catch(err:Error){
		throw err;
	}
}
